package io.renren.utils.util;

import java.util.Locale;

import org.apache.log4j.Logger;

import io.renren.utils.util.message.MessageResources;

public class MessageResourceUtil {
	static Logger logger = Logger.getLogger(MessageResourceUtil.class.getName());
	
	public static final String COMMON_RESOURCE_NAME = "framework";
	public static final String APPLCATION_RESOURCE_NAME = "business";
	
	/////////////
    public static MessageResources  getCommonMessageResources() {
        return MessageResources.getMessageResources(COMMON_RESOURCE_NAME);
    }
    
    public static MessageResources  getApplicationMessageResources() {
        return MessageResources.getMessageResources(APPLCATION_RESOURCE_NAME);
    }
    ///////////////////////////
    public static String getMessage(String key) {
    	return getMessage((Locale) null, key);
    }

    public static String getMessage(String key, Object args[]) {
    	return getMessage((Locale) null, key, args);
    }

    public static String getMessage(String key, Object arg0) {
    	return getMessage((Locale) null, key, arg0);
    }

    public static String getMessage(Locale locale, String key) {    	
    	return getMessage(locale, key, null);
    }

    public static String getMessage(Locale locale, String key, Object arg0) {
		Object args[] = new Object[1];
		args[0] = arg0;
		return getMessage(locale, key, args);
    }

    public static String getMessage(Locale locale, String key, Object args[]) {
    	MessageResources applicationMR = getApplicationMessageResources();
    	String retMsg = applicationMR.getMessage(locale, key, args);
		if(null == retMsg || "".equals(retMsg)) {
			MessageResources commonMR = getCommonMessageResources();
			retMsg = commonMR.getMessage(locale, key, args);
		}
		return retMsg;

    }



    public static void main (String[] args) {
    	try {
    		//PropertyMessageResources.logger.setLevel(org.apache.log4j.Level.DEBUG);
    		MessageResources mr = MessageResourceUtil.getApplicationMessageResources();
    		//MessageResources mr = MessageResources.getCommonMessageResources();
    		//mr.setReturnNull(false);
    		
    		System.out.println("en_US Locale message 1 : "+MessageResourceUtil.getMessage(Locale.US,"TITLE_MSG" ));
    		System.out.println("en_US Locale message 2 (not exist) : "+mr.getMessage(Locale.US,"TITLE_MSG2" ));
    		
    		System.out.println("Default Locale message 1 : "+mr.getMessage(Locale.getDefault(),"FRAMEWORK_LOG_ERROR" ));
    		System.out.println("Default Locale message 2 (not exist) : "+mr.getMessage(Locale.getDefault(),"TITLE_MSG2" ));
    		Object [] parms_zh = new Object[]{"壹贰叁", "标题", "消息", "参数"};
    		System.out.println("Default Locale message 2 (with parameters) : "+mr.getMessage(Locale.getDefault(),"FRAMEWORK_TITLE_MSG_PARM" , parms_zh));
    		
    	} catch (Exception ex) {
    		ex.printStackTrace();
    	}
    }
}
